package com.example.concurrent.chapter06;

import java.util.Optional;
import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @author Create By Liuqz
 * @date 2018/11/22 14:11
 *
 * 复制Thread数组
 */
public class ThreadGroupEnumerateThreads {


    public static void main(String[] args) throws InterruptedException {
        //创建一个ThreadGroup
        ThreadGroup myGroup = new ThreadGroup("MyGroup");

        //创建线程传入threadGroup
        new Thread(myGroup, () -> {
            while (true) {
                try {
                    TimeUnit.SECONDS.sleep(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        },"MyThread").start();

        TimeUnit.MILLISECONDS.sleep(2);

        ThreadGroup mainGroup = Thread.currentThread().getThreadGroup();

        Thread[] list = new Thread[mainGroup.activeCount()];

        int recurseSize = mainGroup.enumerate(list);

        System.out.println(recurseSize);

        recurseSize = mainGroup.enumerate(list, false);

        System.out.println(recurseSize);
    }
}
